import LoadingBar from './index.vue'
import Vue from 'vue'
import config from '../../../config/config.js'

LoadingBar.newInstance = properties => {
  const _props = properties || {}

  const Instance = new Vue({
    data: _props,
    render (h) {
      return h(LoadingBar, {
        props: _props
      })
    }
  })

  const component = Instance.$mount()
  // 添加到页面
  document.body.appendChild(component.$el)

  // loading bar 组件实例
  const loading_bar = Instance.$children[0]
  // 返回参数
  return {
    // 更新组件参数
    update (options) {
      if ('percent' in options) {
        loading_bar.percent = options.percent
      }
      if (options.status) {
        loading_bar.status = options.status
      }
      if ('show' in options) {
        loading_bar.show = options.show
      }
    },
    component: loading_bar,
    // 销毁实例
    destroy () {
      // 移除dom
      document.body.removeChild(document.getElementsByClassName(config.prefix + '-loading-bar')[0])
    }
  }
}

export default LoadingBar
